import requests
from lxml import html


def spider(book_name, book_list=None):
    """ 爬取当当网的数据 """
    if book_list is None:
        book_list = []
    url = 'https://search.dangdang.com/?key={sn}&act=input'.format(sn=book_name)
    # 请求的头部信息，模拟浏览器进行请求，如果不加上会爬起失败
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'
    }

    # 获取html内容
    html_data = requests.get(url, headers=headers).text

    # xpath对象
    selector = html.fromstring(html_data)

    # 找到书本列表
    ul_list = selector.xpath('//div[@id="search_nature_rg"]/ul/li')
    print("共抓取%s条" % len(ul_list))
    for li in ul_list:
        # 标题
        title = li.xpath('a/@title')
        # print(title[0])
        # 购买链接
        link = li.xpath('a/@href')
        # print(link[0])
        # 价格
        price = li.xpath('//span[@class="search_now_price"]/text()')
        # print(price[0].replace('¥', ''))

        book_list.append({
            'title': title[0].strip(),
            'price': price[0].replace('¥', ''),
            'link': link[0].strip()
        })


if __name__ == '__main__':
    sn = '9787115428028'
    spider(sn)
